package com.yx.fsysserver.file.service;

import com.alibaba.fastjson.JSON;
import com.yx.fsysserver.file.domain.VrFileInfo;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author lcf
 * @since 2018/7/4
 */
@Service
public class NewVrService {
    private static Logger log = LoggerFactory.getLogger(FileService.class);

    private String indexTemp;

    @Value("${vr.prefix.path}")
    private String prefixPath;
    @Value("${vr.sendmessage.checkUrl}")
    private String checkUrl;
    @Value("${vr.sendmessage.events.btnLeft}")
    private String btnLeft;
    @Value("${vr.sendmessage.events.btnRight}")
    private String btnRight;
    @Value("${vr.sendmessage.events.btnOK}")
    private String btnOK;
    @Value("${vr.sendmessage.events.left}")
    private String left;
    @Value("${vr.sendmessage.events.right}")
    private String right;
    @Value("${vr.sendmessage.events.up}")
    private String up;
    @Value("${vr.sendmessage.events.down}")
    private String down;
    @Value("${vr.sendmessage.events.front}")
    private String front;
    @Value("${vr.sendmessage.events.back}")
    private String back;

    @Value("${vr.sendmessage.sound.opensound}")
    private String opensound;
    @Value("${vr.sendmessage.sound.clicksound}")
    private String clicksound;
    @Value("${vr.sendmessage.sound.actionsound}")
    private String actionsound;
    @Autowired
    private VrFileInfoService vrFileInfoService;
    /**
     * 初始化
     */
    public void init() {
        System.out.println("run init");
        Resource a4HeadResource = new ClassPathResource("static/vr/index.html");
        if(a4HeadResource.exists()){
            System.out.println("mould_a4_head.tex is exists.");
            try {
                this.indexTemp = FileUtils.readFileToString(a4HeadResource.getFile(),"utf-8");
                System.out.println(this.indexTemp);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            System.out.println("error");
        }
    }

    public String getIndexHtml(long id,HttpServletRequest request){
        this.init();
        //替换title
        String title="";
        //获取VrFileInfo
        VrFileInfo vrFileInfo = vrFileInfoService.selectByPrimaryKey(id);
        if(vrFileInfo != null){
            title = vrFileInfo.getName();
        }
        this.indexTemp = this.indexTemp.replace("$title",title);
        //替换UnityLoader.js
        String jsPath = this.prefixPath + "vr/js/UnityLoader.js";
        this.indexTemp = this.indexTemp.replace("$UnityLoader",jsPath);
        //替换webvrJson
        this.indexTemp = this.indexTemp.replace("$webvrJson","webvrJson?id="+id);
        //替换SendMessage方法的param
        String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
        System.out.println("----url=="+url);

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("checkUrl",url + checkUrl);

        Map<String,Object> events = new HashMap<>();
        events.put("btnLeft","["+btnLeft+"]");
        events.put("btnRight","["+btnRight+"]");
        events.put("btnOK","["+btnOK+"]");
        events.put("left","["+left+"]");
        events.put("right","["+right+"]");
        events.put("up","["+up+"]");
        events.put("down","["+down+"]");
        events.put("front","["+front+"]");
        events.put("back","["+back+"]");
        map.put("events",events);

        map.put("opensound",url + opensound);
        map.put("clicksound",url + clicksound);
        map.put("actionsound",url + actionsound);

        String param = JSON.toJSONString(map);
        log.debug(param);
        this.indexTemp = this.indexTemp.replace("$SendMessageParam",param);
        log.debug(this.indexTemp);


        return this.indexTemp;
    }

}
